package com.ming_za.odupdaterserver.core.service.update.res;

import com.ming_za.odupdaterserver.core.service.update.UpdateState;
import com.ming_za.odupdaterserver.core.sqlite.domain.Res;
import lombok.Getter;
import lombok.Setter;

import java.io.FileInputStream;
import java.util.Objects;

/**
 * 资源请求对象
 * <p>该对象绑定单个资源更新的整个流程</p>
 */
@Getter
public class UpdateRequest {

    /**
     * 状态
     */
    private UpdateState state;

    /**
     * 更新资源
     */
    @Setter
    private Res res;

    /**
     * 文件数据流
     */
    @Setter
    private FileInputStream fileInputStream;

    /**
     * 状态修改的时间
     */
    private long stateUpdateTime;

    public UpdateRequest(UpdateState state, Res res, FileInputStream fileInputStream) {
        this.state = state;
        this.res = res;
        this.fileInputStream = fileInputStream;
    }

    public void setState(UpdateState state){
        this.state = state;
        stateUpdateTime = System.currentTimeMillis();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        UpdateRequest that = (UpdateRequest) o;
        return state == that.state && Objects.equals(res, that.res) && Objects.equals(fileInputStream, that.fileInputStream);
    }

    @Override
    public int hashCode() {
        return Objects.hash(state, res, fileInputStream);
    }
}
